import { defineStore } from 'pinia'
import { getLoginUser, login } from '@/api/user.js'

export const useAppStore = defineStore('app', {
  state: () => ({
    sidebar: {
      opened: true,
      withoutAnimation: false,
    },
    device: 'desktop',
  }),
  getters: {},
  actions: {
    closeSideBar({ withoutAnimation }) {
      this.sidebar.opened = false
      this.sidebar.withoutAnimation = withoutAnimation
    },
    toggleSideBar() {
      this.sidebar.opened = !this.sidebar.opened
      this.sidebar.withoutAnimation = false
    },
    toggleDevice(device) {
      this.device = device
    },
  },
  persist: true,
})

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo: {
      name: '',
    },
  }),
  getters: {},
  actions: {
    async login(data) {
      // todo: 调用登录接口
      this.token = await login(data)
      await this.getUserInfo()
    },
    async getUserInfo() {
      // todo: 调用接口获得当前登录用户
      this.userInfo = await getLoginUser()
    },
    logout() {
      this.token = null
      this.userInfo = {}
    },
  },
  persist: true,
})
